<?php

    class NavigationEngine
    {
       
		private $template;
		private $pageNum;
        private $urlTemplate;
		private $dbProvider;
		
        public function __construct( $mode, $amount )
        {
            $this->template = new TemplateEngine();
			$this->dbProvider = new UserDataProvider();
			
			if ( $mode == "file" )
			{
				if ( isset( $_GET['extension'] ) && isset( $_GET['userEmail'] ) )
				{
					$this->urlTemplate = "show_uploaded_files.php?extension=".$_GET['extension']."&userEmail=".$_GET['userEmail']."&";
				}
				else
				{
					$this->urlTemplate = "show_uploaded_files.php?";
				}
				$this->pageNum = intval( ( ( $amount - 1 ) / USER_AMOUNT + 1 ) );
			}
			else if ( $mode == "users" )
			{
				$this->urlTemplate = "get_user_list.php?";
				$this->pageNum = intval( ( ( $this->getUserAmount() - 1 ) / USER_AMOUNT + 1 ) );
			}
        }
		
		public function getPageNum()
		{
			return $this->pageNum;
		}
		
		public function getPage()
		{
			if ( isset( $_GET['curr_page'] ) )
			{
				return $_GET['curr_page'];
			}
			else
			{
				return 1;
			}
		}
		
		public function getUserAmount()
		{
			$amount = 0;
			$connection = DataBase::getInstance();
            
            $resultArr = $this->dbProvider->getAllUsersFromDb();
			if ( $resultArr )
            {
				$amount = count( $resultArr );
			}
            else
            {
                throw new Exception( $this->template->convertToHtml( "Database is empty!" ) );
            }
            return $amount;
		}
		
		public function printOnePage()
		{
			$pages = array();
			$i = 0;
			$pages[$i]['page'] = 1;
			$pages[$i]['curr'] = "curr";
			$pages[$i]['pageName'] = 1;
			$pages[$i]['link'] = $this->urlTemplate;
			
			return $pages;
		}
		
		public function printFirstPart()
		{
			$pages = array();
			$i = 0;
			$pages[$i]['page'] = 1;
			$pages[$i]['curr'] = "curr";
			$pages[$i]['pageName'] = 1;
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = 2;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = 2;
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = 2;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '>';
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $this->pageNum;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '>>';
			$pages[$i]['link'] = $this->urlTemplate;
			
			return $pages;
		}
		
		
		public function printLastPart()
		{
			$pages = array();
			$i = 0;
			$pages[$i]['page'] = 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '<<';
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $this->pageNum - 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '<';
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $this->pageNum - 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = $this->pageNum - 1;
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $this->pageNum;
			$pages[$i]['curr'] = "curr";
			$pages[$i]['pageName'] = $this->pageNum;
			$pages[$i]['link'] = $this->urlTemplate;

			return $pages;
		}
		
		public function printMiddlePart( $currPage )
		{
			$pages = array();
			$i = 0;
			$pages[$i]['page'] = 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '<<';
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $currPage - 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '<';
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $currPage;
			$pages[$i]['curr'] = "curr";
			$pages[$i]['pageName'] = $currPage;
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $currPage + 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = $currPage + 1;
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $currPage + 1;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '>';
			$pages[$i]['link'] = $this->urlTemplate;
			$i++;
			$pages[$i]['page'] = $this->pageNum;
			$pages[$i]['curr'] = "next";
			$pages[$i]['pageName'] = '>>';
			$pages[$i]['link'] = $this->urlTemplate;
			
			return $pages;
		}
	
	}

?>